<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/3
 * Time: 15:10
 */

namespace app\admin\controller;


use app\admin\service\GroupService;
use app\Request;
use think\App;

class Group extends AdminBase
{
    protected $groupService = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->groupService = new GroupService();
    }

    public function index()
    {
        try {
            $groups = $this->groupService->getAllGroups();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        return view('group/index', compact('groups'));
    }

    public function create()
    {
        $all_permissions = $this->getAllTopPermissions();

        return view('group/create', compact('all_permissions'));
    }

    private function _postData(Request $request)
    {
        return [
            'title'  => $request->post('title', '', 'trim'),
            'status' => $request->post('status', 0, 'intval'),
            'rules'  => $request->post('rules'),
        ];
    }

    public function save(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "请求类型不合法");
        }
        $data          = $this->_postData($request);
        $data['rules'] = implode(',', $data['rules']);

        $result = $this->validate($data, "GroupValidate");
        if (true !== $result) {
            return writeJson(config('status.error'), $result);
        }

        try {
            $res = $this->groupService->addGroup($data);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), "角色添加成功");
        }

        return writeJson(config('status.error'), "角色添加失败");
    }

    public function edit($id)
    {
        try {
            $role_info = $this->groupService->getGroupInfoById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $all_permissions = $this->getAllTopPermissions();

        return view('group/edit', compact('all_permissions', 'role_info'));
    }

    public function update(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "请求类型不合法");
        }
        $data          = $this->_postData($request);
        $data['rules'] = implode(',', $data['rules']);
        $id = $request->post('id', 0, 'intval');

        $result = $this->validate($data, "GroupValidate");
        if (true !== $result) {
            return writeJson(config('status.error'), $result);
        }

        try {
            $res = $this->groupService->updateGroupInfoById($id, $data);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), "角色编辑成功");
        }

        return writeJson(config('status.error'), "角色编辑失败");
    }

    public function delete(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "非法请求类型");
        }

        $id = $request->post('id', 0, 'intval');
        try {
            $res = $this->groupService->deleteById($id);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), '删除成功');
        }

        return writeJson(config('status.error'), '删除失败');
    }
}